package com.star.gc4.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.star.gc4.common.R;
import com.star.gc4.entity.Message;
import com.star.gc4.entity.Msongs;
import com.star.gc4.service.MessageService;
import com.star.gc4.service.MsongsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RestController
@RequestMapping("/message")
public class messageController {
    @Autowired
    private MessageService messageService;

    /*
     * 根据歌单id，songid分页查询结果
     * */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String songid ){
        Page pageinfo =new Page(page,pageSize);
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Message::getSongid,songid);
        queryWrapper.orderByDesc(Message::getId);

        messageService.page(pageinfo,queryWrapper);
        return R.success(pageinfo);
    }

    @PostMapping("/save")
    public R<String> save(HttpServletRequest request, @RequestBody Message message) {
        //Integer userid = (Integer) request.getSession().getAttribute("user");
        Integer userid = 100;
        message.setUserid(userid);
        messageService.save(message);
        return R.success("发表成功");
    }

    @GetMapping("/approve")
    public R<String> delete(HttpServletRequest request, Integer id) {
        //Integer userid = (Integer) request.getSession().getAttribute("user");
        Integer userid = 100;
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(Message::getId,id).eq(Message::getUserids,userid);
        queryWrapper.eq(Message::getId, id);
        Message one = messageService.getOne(queryWrapper);
        String userids = one.getUserids();


        if (one.getUserids() == null||one.getUserids().isEmpty()) {
            userids = "," + userid + ",";
            UpdateWrapper<Message> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("userids", userids);
            updateWrapper.set("favor",1);
            updateWrapper.eq("id", id);

            messageService.update(updateWrapper);

            return R.success("点赞成功");
        }else{

            final String str = "," + userid + ",";
            if (userids.contains(str)) {
                return R.error("已经点过赞了，不能在点了！");
            } else {
                Integer favor = Integer.valueOf(one.getFavor()) ;
                userids = userids + userid + ",";
                UpdateWrapper<Message> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("userids", userids);
                updateWrapper.set("favor",favor+1);
                updateWrapper.eq("id", id);
                messageService.update(updateWrapper);
                return R.success("点赞成功");
            }
        }
    }

}
